package com.cszy.yydqbfq.base


import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.WindowInsets
import androidx.activity.OnBackPressedCallback
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding
import com.cszy.yydqbfq.common.EventUtils

import com.gyf.immersionbar.ktx.immersionBar




import com.kongzue.dialogx.dialogs.WaitDialog
import com.lzx.starrysky.OnPlayProgressListener
import com.lzx.starrysky.OnPlayerEventListener
import com.lzx.starrysky.StarrySky
import kotlinx.coroutines.Job


import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.lang.reflect.ParameterizedType

/**
 * Activity基类
 */
abstract class BaseActivity<VB : ViewBinding, T> : AppCompatActivity()
    {

    protected lateinit var mBinding: VB

    @LayoutRes
    protected open val layoutId = 0

    @SuppressLint("ResourceType")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        immersionBar {
//            statusBarColor(R.color.statubar_color)
//
//            navigationBarColor(R.color.statubar_color)
//            fitsSystemWindows(true)
//        }

        if (isRegisterEvent) {
           EventUtils.register(this)
        }
        initViewDataBinding()
        initView(savedInstanceState)
        initObserve()
        initData()
        initListener()
    }


    override fun onStart() {
        super.onStart()


    }

    abstract fun initView(savedInstanceState: Bundle?)

    open fun initObserve() {}

    open fun initData() {}

    open fun initListener() {}

    open val isRegisterEvent = false

    @Subscribe(threadMode = ThreadMode.MAIN)
    open fun onEventBusCome(event: BaseEvent?) {
        if (event != null) {
            handleEvent(event)
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    open fun onStickyEventBusCome(event: BaseEvent?) {
        if (event != null) {
           // handleStickyEvent(event)
        }
    }

    open fun handleEvent(event: BaseEvent) {}

    open fun handleStickyEvent(event: BaseEvent) {
        if (event.code == 0) {

        }
    }

    /**
     * DataBinding or ViewBinding
     */
    private fun initViewDataBinding() {
        val type = javaClass.genericSuperclass
        if (type is ParameterizedType) {
            val cls = type.actualTypeArguments
                .map { it as Class<*> }
                .first { ViewBinding::class.java.isAssignableFrom(it) }
            when {
                ViewBinding::class.java.isAssignableFrom(cls) && cls != ViewBinding::class.java -> {
                    cls.getDeclaredMethod("inflate", LayoutInflater::class.java).let {
                        @Suppress("UNCHECKED_CAST")
                        mBinding = it.invoke(null, layoutInflater) as VB
                        setContentView(mBinding.root)
                    }
                }
                else -> {
                    if (layoutId == 0) throw IllegalArgumentException("If you don't use ViewBinding, you need to override method layoutId")
                    setContentView(layoutId)
                }
            }
        } else throw IllegalArgumentException("Generic error")
    }

    open fun showLoading(text: String) {
        WaitDialog.show(text)
    }

    open fun dismissLoading() {
        WaitDialog.dismiss()
    }

    override fun onStop() {
        window.decorView.handler?.removeCallbacksAndMessages(null)
        super.onStop()


    }

    override fun onDestroy() {
        super.onDestroy()

        if (isRegisterEvent) {
            EventUtils.unregister(this)
        }
     //   ActivityManager.getInstance().removeActivity(this)
    }

}